/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBean.TX.OutletManagement;

import Web_Helper.EmailSender;
import session.TX.CustomerReservationSBRemote;
import entity.tx.OutletReservation;
import entity.tx.Users;
import java.io.Serializable;
import java.lang.String;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import session.CM.PortalManagementSB;

/**
 *
 * @author Lu Xiaodi
 */
@ManagedBean(name = "reservationManagedBean")
@ViewScoped
public class ReservationMB implements Serializable {

    @EJB
    CustomerReservationSBRemote reservationBean;
    @EJB
    private PortalManagementSB portalManagementSB;
    private String customerEmail;
    private String dateStr;
    private Date selectedDate;
    private List<OutletReservation> reservationByDate = new ArrayList<OutletReservation>();

    public ReservationMB() {
        //outletName = "McdonaldNUS";
    }

    public List<OutletReservation> getReservationByCus() {
        return reservationBean.getReservationByCustomerOutlet(getCustomerEmail(), getOutletId());
    }

    public void recordShowUp(ActionEvent event) {
//        System.out.println("im here");
        Long rId = (Long) event.getComponent().getAttributes().get("rId");
        reservationBean.recordShowUp(getOutletId(), rId); 
    }

    public List<OutletReservation> getReservationByDate() {
        System.out.println(selectedDate); 
        if (selectedDate != null) {
            reservationByDate = reservationBean.getReservationByDateOutlet(selectedDate, getOutletId());
            return reservationByDate;
        } else {
            return new ArrayList<OutletReservation>();
        }
    }

    public void confirmOneReservation(ActionEvent event) {
        // System.out.println("im here");
        OutletReservation selectedRes = (OutletReservation) event.getComponent().getAttributes().get("res");
        reservationBean.confirmReservation(getOutletId(), selectedRes.getId());

        Users user = reservationBean.getReservationUser(selectedRes.getId());

        String emailContent = "Dear " + user.getEmail() + "\n Your FoodSage Reservation with ID " + selectedRes.getId()
                + " is confirmed. \nPlease go to your foodsage personal panel to view the reservation details.";

        String senderEmail = portalManagementSB.getEmailFrom();
        String senderPassword = portalManagementSB.getEmailPassword();
        EmailSender emailsender = new EmailSender(senderEmail, senderPassword);
        String[] recepients = new String[]{user.getEmail()};
        System.out.println(user.getEmail());

        try {
            emailsender.postMail(recepients, "Reservation Confirmation", emailContent, senderEmail);
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage(null, new FacesMessage("Reservation Confirmation email sent successfully", "email sent to customer \n"
                    + user.getEmail()));
        } catch (javax.mail.MessagingException e) {
            return;
        }

    }
    
    public int getNumResConfirmed(){
        int number=0;
        for (OutletReservation r: reservationByDate){
            if (r.getConfirmStatus()!= null){
                if (r.getConfirmStatus()){
                    number ++;
                }
            }
        }
        return number;
    }

    public String getDateStr() {
        return dateStr;
    }

    public void setDateStr(String dateStr) {
        this.dateStr = dateStr;
    }

    public Long getOutletId() {
        return (Long) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("outletId");
    }

    public String getCustomerEmail() {
        return customerEmail;
    }

    public void setCustomerEmail(String customerEmail) {
        this.customerEmail = customerEmail;
    }

    public Date getSelectedDate() {
        return selectedDate;
    }

    public void setSelectedDate(Date selectedDate) {
        this.selectedDate = selectedDate;
    }

    public PortalManagementSB getPortalManagementSB() {
        return portalManagementSB;
    }

    public void setPortalManagementSB(PortalManagementSB portalManagementSB) {
        this.portalManagementSB = portalManagementSB;
    }
}